/*本模块功能:班级管理
        编写者:朱光曜
        编写时间:2017.11.15*/
$(function() {
	var string1 = '<tr>' +
		'<th class="col-lg-2">序号1</th>' +
		'<th class="col-lg-5">班级/申请班</th>' +
		'<th class="col-lg-6">课程</th>' +
		'<th class="col-lg-4">状态</th>' +
		'<th class="col-lg-5">操作</th>' +
		'</tr>';
	/*列表头部*/
	var confirm1 = new Confirm;
	var Alert1 = new Alert;
	var Alert2 = new Alert;

	function locationInformationd(data) { //搜索
		var classes = ""; //班级
		for(key in data.classList) {
			classes += '<option value="1" classId="' + data.classList[key].classId + '">' + data.classList[key].className + '</option>';
		}

		return '<div class="fl inputRight"><label for="departNames">班级名称:&ensp;</label>' +
			'<select id="selectClass" class="publicSelect" >' +
			'<option value="1" classId="">请选择</option>' +
			classes +
			'</select>' +
			'</div>' +
			'<div class="fl inputRight"><label for="departNames">状&ensp;&ensp;&ensp;&ensp;态:&ensp;</label>' +
			'<select id="selectState" class="publicSelect" >' +
			'<option value="">请选择</option>'+
			'<option value="1">已结课</option>' +
			'<option value="0">正在教学中</option>' +
			'</select>' +
			'</div>' +
			'<div class="fl publicBtn searchBtn">搜索</div>';
	}

	function tabelModel(data) { //列表
		var str = "";
		var endMark = "";
		if(data.classCourseList[key].classEndMark == 1) {
			str = "已结课";
			
		}
		if(data.classCourseList[key].classEndMark == 0) {
			str = "正在教学中";
			endMark = "end";
		}
		return '<tr data-num="0" courseId="' + data.classCourseList[key].courseId + '" classId="' + data.classCourseList[key].classId + '">' +
			'<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
			'<td class="col-lg-10"  title="' + data.classCourseList[key].className + '">' + data.classCourseList[key].className + '</td>' +
			'<td class="col-lg-10"  title="' + data.classCourseList[key].courseName + '">' + data.classCourseList[key].courseName + '</td>' +
			'<td class="col-lg-10"  title="">' + str + '</td>' +
			'<td class="col-lg-4">' +
			'<a href="javascript:;" class="guitarLessons ' + endMark + '">结课</a>' +
			// '<a href="javascript:;" class="viewVirtual">查看虚拟机</a>' +
			'</td>' +
			'</tr>';

	}
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
		
		});
	$(document).ajaxStop(function(){
		loading1.hide();
	
		});
	var nowData = 1;
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			this.data_content = {}; //搜索集
			this.tab_content = {}; //列表集
			this.parameter = {
				"pageNow": 1,
				"pageSize": 10
			};

			this.postData(this.parameter, nowData);

		},
		postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
			var self = this;
			var parameter = par_data;

			$.ajax({
				type: "post",
				url: "skipTeaEndClassList",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},
				success: function(data, status, xhr) {
					var new_data = JSON.parse(data);
					console.log(new_data,333);
                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "skipTeaEndClassList",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取课程
                                        var msgNew = JSON.parse(msg);
                                        self.tab_content = msgNew;

                                        self.tab_add();
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
					}else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
					}

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		add: function() { //搜索追加
			var self = this;
			var data = self.data_content;

			$("#searchCriteria").html(locationInformationd(data));

			
			$(".funBox .searchBtn").off("click").click(function() { //搜索点击

				
				var classid = $("#selectClass option:selected").attr("classid");
				var classEndMark = $("#selectState option:selected").attr("value");
				var nowData = 1;
				var parameter = {
					
					"classId": classid,
					"classEndMark":classEndMark,
					"pageNow": 1,
					"pageSize": 10
				};
				
				//遍历删除空元素
				for(i in parameter){
					
					if(parameter[i]==null||parameter[i]==""){
						delete parameter[i];
					}
					if(!isNaN(parameter[i])){
						parameter[i]=Number(parameter[i]);
					}
				}


				
				self.postData(parameter, nowData, 1);
			});

		},
		tab_add: function() { //列表追加

			var self = this;
			var data = self.tab_content;
			var table = string1;

			//data.selectList={};

			var i = 0;
			for(key in data.classCourseList) {
				i++;
			}

			if(i > 0) {

				for(key in data.classCourseList) {

					table += tabelModel(data);
				}

				$(".mainContent .tableBox").html(table);
			} else {
				var trImg = "<tr><td colspan='5'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
				$(".mainContent .tableBox").html(string1 + trImg);
			}
            
			//设置列表宽度(最大为9)
			var numMax = 0;
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
				numMax += num1;
			});
			
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
			
				$(this).css("width", num1 / numMax * 100 + "%");
			});
            
            
            
            
			$(".viewVirtual").off("click").on("click", function() { //详情跳页
				var json = JSON.stringify({
					"pageName": "stuVirrAllocationDetail",
					"paramData": {
						"classId": parseInt($(this).parents("tr").attr("classId"))
					}
				});
			
				window.location.href = "skipPage?jsonStr=" + json;
			});
			$(".end").off("click").on("click", function(event) { //结课信息

				
				var courseId = $(this).parents("tr").attr("courseId");
				var classId= $(this).parents("tr").attr("classId");
				var method1=function(){
					
				
				var endCourseClassParameters = {
					"courseId": courseId,
					"classEndMark":1,
					"classId":classId,
				};

				
				$.ajax({
					type: "post",
					url: "endCourseClass",
					async: true,
					data: {
						"jsonStr": JSON.stringify(endCourseClassParameters)
					},

					success: function(msg) {
						var courseIds = courseId;
						var msgNew = JSON.parse(msg);

						if(msgNew.result == "success") {
							self.alert1("结课提交成功");
						} else {
							//self.alert1("结课失败");
							if(msgNew.failMsg){
								self.alert1(msgNew.failMsg);
							}else{
								self.alert1("结课失败");
							}

						}

					},
					error: function(data, status, xhr) {
						var json = JSON.stringify({
							"pageName": "error"
					    });
						window.location.href = "skipPage?jsonStr=" + json;

					}
				});
				};
				self.confirmEnd("当前班级正在授课中，确定要结课吗？请慎重操作",method1);
				event.stopPropagation();
			});
			
		},
		alert1: function(content1) { //alert提示

			Alert1.contenthead = "提示";
			Alert1.content = content1;

			Alert1.success = function() {
				window.location.reload();
				Alert1.hide();
			};
			

			Alert1.init();
			Alert1.show();
		},
		confirmEnd:function(content1,method1){//confirm提示
			confirm1.contenthead="提示";
			confirm1.content = content1;
			confirm1.success=method1;
			confirm1.init();
			confirm1.show();
			
		}
	};

	index.init();

});